package com.mikufufu.blog.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mikufufu.blog.common.annotation.OperationLog;
import com.mikufufu.blog.common.enums.ModuleType;
import com.mikufufu.blog.common.enums.OperationType;
import com.mikufufu.blog.model.dto.api.CategoryParam;
import com.mikufufu.blog.model.entity.Category;
import com.mikufufu.blog.model.vo.admin.CategoryVO;
import com.mikufufu.blog.service.CategoryService;
import com.mikufufu.blog.utils.BeanUtil;
import com.mikufufu.blog.utils.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.mikufufu.blog.common.entity.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Api(tags = "文章分类接口")
public class CategoryAdminController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/detail")
    @ApiOperation(value = "获取分类详情", notes = "获取分类详情")
    @ApiImplicitParam(name = "id", value = "分类id", required = true)
    public  AjaxResult<Category> detail(Integer id) {
        return AjaxResult.data(categoryService.getById(id));
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取分类列表",notes = "获取分类列表")
    public AjaxResult<List<CategoryVO>> list() {
        return AjaxResult.data(BeanUtil.copyList(categoryService.list(), CategoryVO.class));
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页获取分类列表",notes = "分页获取分类列表")
    public AjaxResult<IPage<Category>> page(CategoryParam param) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(param.getType())){
            queryWrapper.eq(Category::getCategoryType,param.getType());
        }
        return AjaxResult.data(categoryService.page(param.getPage(),queryWrapper));
    }

    @GetMapping("/tree")
    @ApiOperation(value = "获取分类列表（树形结构）", notes = "获取分类列表（树形结构）")
    public AjaxResult<List<CategoryVO>> tree(Integer type) {
        return AjaxResult.data(categoryService.getCategoryTree(type));
    }

    @OperationLog(module = ModuleType.ARTICLE, type = OperationType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增分类", notes = "新增分类")
    public AjaxResult<Boolean> add(@RequestBody Category category) {
        return AjaxResult.status(categoryService.save(category));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改分类",notes = "修改分类")
    public AjaxResult<Boolean> update(@RequestBody Category category) {
        return AjaxResult.status(categoryService.updateById(category));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除分类", notes = "删除分类")
    @ApiImplicitParam(name = "id", value = "分类id", required = true)
    public AjaxResult<Boolean> delete(Integer id) {
        return AjaxResult.status(categoryService.removeById(id));
    }

}
